export function onResizeEnd(call) {
    function debounce(func) {
        var timer;
        return function (event) {
            if (timer) clearTimeout(timer);
            timer = setTimeout(func, 100, event);
        };
    }

    window.addEventListener('resize', debounce(call))
}

export function getHeight() {
    return window.innerHeight
}

export function getWidth() {
    var width = document.documentElement.offsetWidth
    return width > 750 ? 750 : width
}

export function px2rem($px, $fontSize) {
    var $_fontSize = $fontSize || 100
    return ($px / $_fontSize / 2) + "rem"
}


var imgExt = new Array(".png", ".jpg", ".jpeg", ".bmp", ".gif");//图片文件的后缀名
var docExt = new Array(".doc", ".docx");//word文件的后缀名
var xlsExt = new Array(".xls", ".xlsx");//excel文件的后缀名
var cssExt = new Array(".css");//css文件的后缀名
var jsExt = new Array(".js");//js文件的后缀名

//获取文件名后缀名
String.prototype.extension = function () {
    var ext = null;
    var name = this.toLowerCase();
    var i = name.lastIndexOf(".");
    if (i > -1) {
        var ext = name.substring(i);
    }
    return ext;
}

//判断Array中是否包含某个值
Array.prototype.contain = function (obj) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === obj)
            return true;
    }
    return false;
};

function typeMatch(type, filename) {
    var ext = filename.extension();
    if (type.contain(ext)) {
        return true;
    }
    return false;
}

export function isImage(file) {
    return typeMatch(imgExt, file.name)
}

export function previewImage(file, dom) {
    if (isImage(file)) {
        var reader = new FileReader();
        reader.onload = function (e) {
            dom.src = e.target.result
        }
        reader.readAsDataURL(file);
    }
}
